
let srcS = [ //---储存动画信息
    {
        id: "breath",
        src: "common/images/dynamic/breath/breath ",
        len: 90,
        setInt: 70,
        width: 720,
        height: 1000,
        x: 0,
        y: 0,
        format: '.png'
    },
    {
        id: "neck",
        src: "common/images/dynamic/neck/neck ",
        len: 22,
        setInt: 50,
        width: 720,
        height: 1000,
        x: -5,
        y: 85,
        format: '.png'
    },
    {
        id: "blink",
        src: "common/images/dynamic/blink/blink ",
        len: 37,
        setInt: 70,
        width: 720,
        height: 1000,
        x: 0,
        y: 0,
        format: '.png'
    },
    {
        id: "wave",
        src: "common/images/dynamic/wave/newwave ",
        len: 30,
        setInt: 85,
        width: 720,
        height: 1000,
        x: 0,
        y: 0,
        format: '.png'
    },
    {
        id: "sway",
        src: "common/images/dynamic/sway/sway ",
        len: 22,
        setInt: 120,
        width: 720,
        height: 1000,
        x: 0,
        y: 0,
        format: '.png'
    },
]

let bitmapList = [];
export function preLoad(id, w, h, cb) {
    let temp;
    let obj = srcS.find(e => e.id === id);
    let len = obj.len;
    let list = [], bitmap;
    for (let i = 0; i < len; i++) {
        let j = '';
        if (i > 9) j = '';
        let str = j + i.toString();
        let img = new Image();
        img.src = obj.src + str + obj.format; //---设置Image对象路径，这里图片的路径编号是00,01,02...10...39
        var offscreen = new OffscreenCanvas(w, h);
        var offscreenCanvasCtx = offscreen.getContext("2d");
        offscreenCanvasCtx.drawImage(img, obj.x, obj.y, w, h);
        bitmap = offscreen.transferToImageBitmap();
        list.push(bitmap)
    }
    temp = {
        id: id,
        data: list,
        w: w,
        h: h
    }
    bitmapList.push(temp) //---储存load好的图片
    console.info(id)
    if(id=='breath')cb(temp);
    else cb()
}

export function getBreathDynamic(){
    return
}
export class playAnimation { //---创建一个动画对象---可以复用
    #info;
    id;
    x;
    y;
    w;
    h;
    len;
    interval;
    #ctx;

    constructor(id, ctx, info = null) {
        if (info)this.#info = info;
        this.id = id;
        this.#ctx = ctx;
        this.setInfo()
        this.x = this.#info.x;
        this.y = this.#info.y;
        this.w = this.#info.width;
        this.h = this.#info.height;
        this.interval = this.#info.setInt;
        this.len = this.#info.len;
    }

    getInfo() {
        return this.#info;
    }
    setHW(w, h) {
        this.w = w;
        this.h = h;
    }
    imgLoad(cb) {
        let imgArray = []; //---存储Image对象
        let len = this.#info.len;
        for (let i = 0; i < len; i++) {
            let j = '';
            if (i > 9) j = '';
            let str = j + i.toString();
            let img = new Image();
            img.src = this.#info.src + str + this.#info.format; //---设置Image对象路径，这里图片的路径编号是00,01,02...10...39
            imgArray.push(img);
        }
        cb(imgArray, len);
    }
    findBitmap(w, h) { //---
        for (let i in bitmapList) {
            if (bitmapList[i].w === w && bitmapList[i].id === this.id && bitmapList[i].h === h) {
                return bitmapList[i]
            }
        }
        return -1;
    }
    play(w=this.w, h=this.h) {
        let Iv;
        let obj = this.findBitmap(w, h); //----查找是否存在已经渲染的动作
        //        console.info("obj = " + obj)
        if (obj != -1) { //----已有离屏渲染的播放
            return new Promise((resolve) => {
                let i = 0;
                let bitmap = obj.data;
                Iv = setInterval(() => {
                    if (i < this.len) {
                        if (i !== 0)this.#ctx.clearRect(this.#info.x, this.#info.y, w, h); //---不断绘制新图，清除旧图
                        this.#ctx.transferFromImageBitmap(bitmap[i]); //---播放离屏渲染的动画
                        ++i;
                    } else {
                        //                        this.#ctx.clearRect(this.x, this.y, this.w, this.h)
                        clearInterval(Iv);
                        resolve(false);
                    }
                }, this.#info.setInt);
            })
        }
        else //---无离屏渲染的播放
        return new Promise((resolve) => {
            var i = 0;
            let len, imgArray;
            this.imgLoad((imgArray_, len_) => {
                imgArray = imgArray_; //---设置drawImage
                len = len_;
                Iv = setInterval(() => {
                    if (i < len) {
                        if (i == 0) {
                            this.#ctx.clearRect(this.x, this.y, this.w, this.h);
                            this.w = w;
                            this.h = h;
                        }
                        else {
                            this.#ctx.clearRect(this.x, this.y, this.w, this.h); //---不断绘制新图，清除旧图
                        }

                        this.#ctx.drawImage(imgArray[i], this.x, this.y, this.w, this.h);
                        ++i;
                    } else {
                        this.#ctx.clearRect(this.x, this.y, this.w, this.h);
                        //                        this.#ctx.drawImage(imgArray[0], this.x, this.y, this.w, this.h); //---可选择保留结尾动作
                        clearInterval(Iv);
                        resolve(false);
                    }
                }, this.interval);
            })
        })
    }
    setInfo() { //---找到srcS中对应的动画对象
        this.#info = srcS.find(e => e.id === this.id);
        if(this.#info)console.info("find success")
        else console.info(this.id+" find lose")
    }
}
